import Player from './modules/Player.js';
import Stock from './modules/Stock.js';
import { drawStrokeRect, drawFillRect, drawCircle, drawFillCircle, drawFillText } from './utils/draw.js';
import { generateUUID, getRandomInt, isCircleColliding } from './utils/tool.js';
import PlayerController from './utils/playerController.js';
import Star from './modules/Star.js';

let canvas;
let ctx;
let playerController;
let AnimationFrameHandle;
let stockCount = 0;
let playerCount = 1;
let starCount = 5;

window.onload = () => {
    // 加载此文件时，隐藏部分按钮
    ['trainAI', 'loadAI', 'AIPlay'].forEach(item => {
        let ele = document.getElementById(item);
        ele && (ele.style.display = 'none');
    });

    canvas = document.getElementById('canvas');
    ctx = canvas.getContext('2d');

    console.log('tf', tf);
    console.log('tf.sequential()', tf.sequential());

    // 将整个项目的数据都添加都window对象上,这样整个项目都容易获取到
    window.GameData = {
        /** 画布 */
        canvas,
        /** 画笔 */
        ctx,
        /** 怪物的数量 */
        stockArr: [],
        /** 玩家的数量 */
        playerArr: [],
        /** 星星的数量 */
        starArr: [],
    };
};

function mian() {
    if (window.GameData.playerArr.length === 0) {
        console.log('游戏结束');
        drawFillRect(ctx, 0, 0, canvas.width, canvas.height, 'gray');
        drawFillText(ctx, canvas.width / 2 - 90, canvas.height / 2, `游戏结束`, 'red', 50);
        return;
    }
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 绘制对象
    for (const stock of window.GameData.stockArr) {
        stock.update();
        stock.draw();
    }
    for (const player of window.GameData.playerArr) {
        player.update();
        player.draw();
    }
    for (const star of window.GameData.starArr) {
        star.update();
        star.draw();
    }

    // 更新玩家控制器
    playerController.update();

    // 玩家与石头或星星相撞，则淘汰玩家（一个玩家时，直接结束游戏）
    for (const stock of window.GameData.stockArr) {
        for (const player of window.GameData.playerArr) {
            if (isCircleColliding(stock, player)) {
                let index = window.GameData.playerArr.findIndex(item => item.id === player.id);
                window.GameData.playerArr.splice(index, 1);
            }
        }
    }

    // 如果玩家和星星相撞，则给玩家加分
    for (const star of window.GameData.starArr) {
        for (const player of window.GameData.playerArr) {
            if (isCircleColliding(star, player)) {
                player.score++;
                let index = window.GameData.starArr.findIndex(item => item.id === star.id);
                window.GameData.starArr.splice(index, 1);
            }
        }
    }

    // 如果星星数量不足的话，则不足星星数量
    if (window.GameData.starArr.length < starCount) {
        let diff = starCount - window.GameData.starArr.length;
        for (let i = 0; i < diff; i++) {
            window.GameData.starArr.push(new Star(ctx, getRandomInt(50, canvas.width - 50), getRandomInt(50, canvas.height - 50)));
        }
    }

    if (window.GameData.playerArr.length !== 0) {
        // 更新UI
        drawFillText(ctx, 20, 20, `玩家0分数：${window.GameData.playerArr[0].score}`, 'red');
    }

    AnimationFrameHandle = requestAnimationFrame(mian);
}

/** 初始化游戏数据 */
function initGame() {
    window.GameData.stockArr = [];
    window.GameData.playerArr = [];
    window.GameData.starArr = [];
    playerController = null;

    // 生成对象（调试AI中，暂不生成石头）
    for (let i = 0; i < stockCount; i++) {
        window.GameData.stockArr.push(new Stock(ctx, getRandomInt(50, canvas.width - 50), getRandomInt(50, canvas.height - 50)));
    }
    for (let i = 0; i < playerCount; i++) {
        window.GameData.playerArr.push(new Player(ctx, getRandomInt(50, canvas.width - 50), getRandomInt(50, canvas.height - 50)));
    }
    for (let i = 0; i < starCount; i++) {
        window.GameData.starArr.push(new Star(ctx, getRandomInt(50, canvas.width - 50), getRandomInt(50, canvas.height - 50)));
    }

    // 生成玩家控制器
    playerController = new PlayerController(window.GameData.playerArr[0]);
}

/** 开始游戏 */
function startGame() {
    cancelAnimationFrame(AnimationFrameHandle);
    initGame();
    mian();
}

document.getElementById('beignGame').addEventListener('click', () => {
    startGame();
});
